﻿using System;
using System.Collections.Generic;
//using System.Diagnostics;
using Csu.Modsim.ModsimModel; 
using ASquared.SymbolicMath;

namespace iwpm.optimization
{
    public class HydroFunctionDebuggerEventArgs : MessageEventArgs
    {
        private Symbol _fxn;
        public readonly string headers = "StartDate Date,End Date,Iteration,Variables,FxnOfVariables"; //,StackTrace"; 

        public HydroFunctionDebuggerEventArgs(Model model, Symbol fxn, string[] variables)
        {
            _fxn = fxn;
            //StackTrace t = new StackTrace();
            base.Message = model.mInfo.CurrentBegOfPeriodDate.ToString() + ","
                + model.mInfo.CurrentEndOfPeriodDate.ToString() + ","
                + model.mInfo.Iteration.ToString() + ","
                + string.Join(" ", variables) + ","
                + FxnOfVariables(variables).ToString();
            //  + "\n" + t.ToString();
        }

        public Symbol Fxn
        {
            get
            {
                return _fxn;
            }
        }

        public Symbol FxnOfVariables(string[] variables)
        {
            List<string> vars = _fxn.Variables();
            foreach (string variable in variables)
                if (vars.Contains(variable))
                    vars.Remove(variable);
            string[] otherVars = vars.ToArray();
            double[] zeros = Array.ConvertAll(otherVars, elem => 0.0);
            return _fxn.Subs(otherVars, zeros);
        }

    }
}
